#import "BWAppController.h"

#import "BWColorset.h"
#import "BWSymbol.h"

#import "BWPreferenceWindowController.h"
#import "BWPannerWindowController.h"
#import "BWThreadWindowController.h"
#import "BWSymbolWindowController.h"
#import "BWLayerWindowController.h"
#import "BWColorKeyWindowController.h"
#import "BWGridWindowController.h"
#import "BWMetadataWindowController.h"

static BWAppController *g_globalController;

@implementation BWAppController

+ (BWAppController *) globalController
{
    return (g_globalController);

} // globalController



- (void) loadColorsets
{
    NSBundle *bundle = [NSBundle mainBundle];

    colorsets = [[NSMutableArray alloc] init];

    NSArray *paths;
    paths = [bundle pathsForResourcesOfType: @"bwcdoc"
                    inDirectory: @""];

    NSEnumerator *enumerator = [paths objectEnumerator];
    
    NSString *path;
    while (path = [enumerator nextObject]) {
        NSData *data = [NSData dataWithContentsOfFile: path];
        BWColorset *colorset;
        colorset = [NSUnarchiver unarchiveObjectWithData: data];
        [colorsets addObject: colorset];
    }
    
} // loadColorsets


- (void) loadSymbols
{
    NSBundle *bundle = [NSBundle mainBundle];

    symbols = [[NSMutableArray alloc] init];

    NSArray *paths;
    paths = [bundle pathsForResourcesOfType: @"bwstitch"
                    inDirectory: @""];

    NSEnumerator *enumerator = [paths objectEnumerator];
    
    NSString *path;
    while (path = [enumerator nextObject]) {
        NSData *data = [NSData dataWithContentsOfFile: path];

        // each bwstitch file is an array of BWSymbols
        NSArray *array;
        array = [NSUnarchiver unarchiveObjectWithData: data];
        [symbols addObjectsFromArray: array];
    }

} // loadSymbols


- (void) setDefaultPrefs
{
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

    [dictionary setObject: [NSNumber numberWithBool: YES]
                forKey: @"presetAuthor"];

    [dictionary setObject: @""
                forKey: @"defaultCopyright"];

    [[NSUserDefaults standardUserDefaults]
        registerDefaults: dictionary];

} // setDefaultPrefs


- (id) init
{
    if (self = [super init]) {
        g_globalController = self;
        [self loadColorsets];
        [self loadSymbols];
        [self setDefaultPrefs];
    }

    return (self);

} // init


- (void) dealloc
{
    [colorsets release];
    [super dealloc];

} // dealloc


- (NSArray *) colorsets
{
    return (colorsets);
} // colorsets


- (NSArray *) symbols
{
    return (symbols);
} // symbols

- (BWSymbol *) symbolForPackageName: (NSString *) packageName
                           symbolId: (int) symbolId
{
    BWSymbol *returnSymbol = nil;

    NSEnumerator *enumerator = [symbols objectEnumerator];
    BWSymbol *symbol;

    while (symbol = [enumerator nextObject]) {

        if ([[symbol packageName] isEqualTo: packageName]
            && [symbol symbolId] == symbolId) {
            returnSymbol = symbol;
            break;
        }
    }

    return (returnSymbol);

} // symbolForPackageName



- (BWColorset *) colorsetForManufacturerName: (NSString *) name
{
    BWColorset *foundSet = nil;

    NSEnumerator *enumerator = [colorsets objectEnumerator];
    
    BWColorset *set;
    while (set = [enumerator nextObject]) {
        if ([[set manufacturerName] isEqualToString: name]) {
            foundSet = set;
            break;
        }
    }

    return (foundSet);

} // colorsetForManufacturerName


- (IBAction) showPreferenceWindow: (id) sender
{
    BWPreferenceWindowController *controller;
    controller = [BWPreferenceWindowController sharedController];

    [controller show];

} // showPreferenceWindow


- (IBAction) toggleThreadWindow: (id) sender
{
    BWThreadWindowController *controller;
    controller = (BWThreadWindowController *)
        [BWThreadWindowController sharedController];

    [controller showOrHide];

    [controller setColorsets: colorsets];

} // toggleThreadWindow


- (IBAction) togglePannerWindow: (id) sender
{
    [[BWPannerWindowController sharedController] showOrHide];

} // togglePannerWindow


- (IBAction) toggleSymbolWindow: (id) sender
{
    BWSymbolWindowController *controller;
    controller = [BWSymbolWindowController sharedController];

    [controller showOrHide];

    [controller setSymbols: symbols];

} // toggleSymbolWindow


- (IBAction) toggleLayerWindow: (id) sender
{
    BWLayerWindowController *controller;
    controller = [BWLayerWindowController sharedController];

    [controller showOrHide];

} // toggleLayerWindow


- (IBAction) toggleColorKeyWindow: (id) sender
{
    BWColorKeyWindowController *controller;
    controller = [BWColorKeyWindowController sharedController];

    [controller showOrHide];

} // toggleColorKeyWindow


- (IBAction) showColorKeyWindow: (id) sender
{
    BWColorKeyWindowController *controller;
    controller = [BWColorKeyWindowController sharedController];

    [controller show];

} // showColorKeyWindow


- (IBAction) toggleGridWindow: (id) sender
{
    BWGridWindowController *controller;
    controller = [BWGridWindowController sharedController];

    [controller showOrHide];

} // toggleGridWindow


- (IBAction) toggleDocumentInformationWindow: (id) sender
{
    BWMetadataWindowController *controller;
    controller = [BWMetadataWindowController sharedController];
    
    [controller showOrHide];

} // toggleDocumentInformationWindow


@end // BWAppController

